// src/router/index.ts
import { createWebHistory, createRouter } from 'vue-router'
import { useUserStore } from '@/stores'
import { ElMessage } from 'element-plus'

// 静态路由配置
const routes = [
	{
		path: '/',
		name: 'layout',
		component: () => import('@/Layout/layoutPage.vue'),
		children: [
			{
				path: '',
				name: 'home',
				component: () => import('@/views/Home/homePage.vue'),
				meta: { title: '首页' }
			},
			{
				path: 'teacher/score/input',
				name: 'teacher-score-input',
				component: () => import('@/views/Teacher/scoreInput.vue'),
				meta: { title: '成绩录入', allowUser: [2, 5] }
			},
			{
				path: 'teacher/score/distribution',
				name: 'teacher-score-distribution',
				component: () => import('@/views/Teacher/scoreDistribution.vue'),
				meta: { title: '成绩分布', allowUser: [1, 2, 3, 5] }
			},
			{
				path: 'teacher/subject/analysis',
				name: 'teacher-subject-analysis',
				component: () => import('@/views/Teacher/scoreAnalysis.vue'),
				meta: { title: '试题分析', allowUser: [2, 5] }
			},
			{
				path: 'teacher/subject/analysis/upload',
				name: 'teacher-subject-analysis-upload',
				component: () => import('@/views/Teacher/components/analysis.vue'),
				meta: { title: '试题分析提交', allowUser: [2, 5] }
			},
			{
				path: 'counsellor/classScore',
				name: 'counsellor-class-score',
				component: () => import('@/views/Counsellor/classScore.vue'),
				meta: { title: '班级成绩', allowUser: [2, 3, 5] }
			},
			{
				path: 'counsellor/studentList',
				name: 'counsellor-student-list',
				component: () => import('@/components/User/userList.vue'),
				meta: { title: '班级学生', allowUser: [3, 5] }
			},
			{
				path: 'my/score',
				name: 'my-score',
				component: () => import('@/views/My/myScore.vue'),
				meta: { title: '个人成绩', allowUser: [1, 5] }
			},
			{
				path: 'my/info',
				name: 'my-info',
				component: () => import('@/views/My/userInfo.vue'),
				meta: { title: '个人信息', icon: 'User', allowUser: [1, 2, 3, 4, 5] }
			},
			{
				path: 'backend/score/template',
				name: 'backend-score-template',
				component: () => import('@/views/Backend/scoreReport.vue'),
				meta: { title: '成绩报告管理', allowUser: [4, 5] }
			},
			{
				path: 'backend/user/manage',
				name: 'backend-user-manage',
				component: () => import('@/views/Backend/userManage.vue'),
				meta: { title: '用户管理', allowUser: [4, 5] }
			},
			{
				path: 'backend/education/manage',
				name: 'backend-education-manage',
				component: () => import('@/views/Backend/educationManage.vue'),
				meta: { title: '班级管理', allowUser: [4, 5] }
			},
			{
				path: 'backend/system/manage',
				name: 'backend-system-manage',
				component: () => import('@/views/Backend/systemManage.vue'),
				meta: { title: '系统管理', allowUser: [4, 5] }
			},
			// {
			// 	path: 'backend/page/manage',
			// 	name: 'backend-page-manage',
			// 	component: () => import('@/views/Backend/pageManage.vue'),
			// 	meta: { title: '页面管理', allowUser: [4, 5] }
			// },
			{
				path: 'scoreInfo/:studentId',
				name: 'score-info',
				component: () => import('@/components/User/userScore.vue'),
				meta: { title: '学生成绩详情', allowUser: [1, 2, 3, 4, 5] }
			},
			{
				path: 'classScoreInfo/:classId',
				name: 'class-score-info',
				component: () => import('@/components/Class/classScore.vue'),
				meta: { title: '班级成绩详情', allowUser: [2, 3, 4, 5] }
			},
			{
				path: 'userInfo/:userId',
				name: 'user-info',
				component: () => import('@/components/User/userInfo.vue'),
				meta: { title: '用户详情页', allowUser: [1, 2, 3, 4, 5] }
			},
			{
				path: 'teacher/userList',
				name: 'teacher-user-list',
				component: () => import('@/components/User/userList.vue'),
				meta: { title: '授课学生', icon: 'User', allowUser: [2, 5] }
			},
			{
				path: 'teacher/score/import',
				name: 'teacher-score-import',
				component: () => import('@/views/Teacher/excelImport.vue'),
				meta: { title: '成绩导入', allowUser: [2, 5] }
			},
			{
				path: 'my/feedback',
				name: 'my-feedback',
				component: () => import('@/views/My/feedBack.vue'),
				meta: { title: '意见反馈', icon: 'Warning', allowUser: [1, 2, 3, 5] }
			},
			{
				path: 'backend/feedback/manage',
				name: 'backend-feedback-manage',
				component: () => import('@/views/Backend/feedBackManage.vue'),
				meta: { title: '反馈管理', icon: 'Delete', allowUser: [4, 5] }
			},
			{
				path: 'backend/subject',
				name: 'backend-subject',
				component: () => import('@/views/Backend/subjectManage.vue'),
				meta: { title: '科目管理', icon: 'Setting', allowUser: [4, 5] }
			}
		]
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/Login/loginPage.vue'),
		meta: { title: '登录页' }
	},
	{
		path: '/:pathMatch(.*)*',
		redirect: '/'
	}
]

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL + 'sms'),
	routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
	const userStore = useUserStore()
	const userInfo = userStore.userInfo

	// 登录验证
	if (!userInfo || !userInfo.token) {
		// 如果不是前往登录页，则重定向到登录页
		if (to.name !== 'login') {
			ElMessage.error('请先登录')
			return next('/login')
		}
		return next()
	}

	// 权限验证
	const userType = userInfo.userInfo?.userTypeId || 0
	const allowUser = (to.meta.allowUser as number[]) || [1, 2, 3, 4, 5]

	if (allowUser && !allowUser.includes(userType)) {
		ElMessage.error('您没有访问该页面的权限')
		return next('/')
	}

	next()
})

export default router
